<template>
  <div class="detail">
    <product-param v-bind:title="product.name"></product-param>
    <div class="wrapper">
      <div class="container clearfix">
        <div class="swiper">          
          <img :src="'http://localhost:8080'+product.goodsCoverImg" alt="" />
          <swiper :options="swiperOption">
            <swiper-slide v-for="(item, index) of albumPics" :key="index"
              ><img :src="item" alt="" />
            </swiper-slide>
            <div class="swiper-pagination" slot="pagination"></div>
          </swiper>
        </div>
        <div class="content">
          <h2 class="item-title">{{ product.goodsName }}</h2>
          <p class="item-info">{{ product.goodsIntro}}</p>
          <div class="item-price"><span>价格：</span>{{ product.sellingPrice }}元</div>
          <div class="line"></div>

          <span>数量：</span>
          <el-input-number v-model="count" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>

          <div class="item-total">
            <div class="phone-total">总计：{{total}}元</div> 
          </div> 
          <div class="btn-group">
            <a href="javascript:;" class="btn btn-large fl" style="background-color: #1baeae;" @click="addToCart"
              >加入购物车</a
            >
            <!-- <a href="javascript:;" class="btn btn-large btn-buy fl" style="background-color: #1baeae; color: #f3f3f3">立即购买</a> -->
          </div>

          <div class="after-sale-info">
            <span
              ><a
                href="javascript:void(0);"
                v-if="serviceIds.includes('1')"
                class="support"
              >
                <i class="fa fa-check-circle-o f2" aria-hidden="true"></i>
                <em>无忧退货</em>
              </a>
            </span>
            <span
              ><a
                href="javascript:void(0);"
                v-if="serviceIds.includes('2')"
                class="support"
              >
                <i class="fa fa-check-circle-o f2" aria-hidden="true"></i>
                <em>快速退款</em>
              </a>
            </span>
            <span
              ><a
                href="javascript:void(0);"
                v-if="serviceIds.includes('3')"
                class="support"
              >
                <i class="fa fa-check-circle-o f2" aria-hidden="true"></i>
                <em>免费包邮</em>
              </a>
            </span>
          </div>
        </div>
      </div>
    </div>

    <div class="price-info">
      <div class="container">
        <el-menu :default-active="activeIndex" class="el-menu-demo" 
        mode="horizontal" 
        @select="handleSelect" 
        active-text-color="#1baeae" 
        background-color="#f3f3f3">
        <el-menu-item index="1">商品介绍</el-menu-item>
        <el-menu-item index="2">用户评价</el-menu-item>
        </el-menu>
        
        <template v-if="activeIndex==='1'">
          <div class="attributes">
            <span>{{product.goodsDetailContent}}</span>
            <ul class="attributes-list">
              <li
                v-for="(item, index) in product.goodsCarouselList"
                :key="index"
              >
                {{ item }}:&nbsp;
              </li>
            </ul>
          </div>
        </template>
        <template v-if="activeIndex==='2'">
          <comment :reviews="reviews"></comment>
        </template>
          
 
      </div>
    </div>
    <service-bar></service-bar>
  </div>
</template>
<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
import ProductParam from "./../components/ProductParam";
import ServiceBar from "./../components/ServiceBar";
import "swiper/dist/css/swiper.css";
import {getDetail} from './../service/good';
import {getComment} from './../service/comment';
import {addCart} from './../service/cart';
import Comment from './../components/Comment' ;
export default {
  name: "detail",
  data() {
    return {
      id: this.$route.params.id, //获取商品ID
      err: "",
      version: 1, //商品版本切换
      product: {}, //商品信息
      count:1,
      total:0,
      swiperOption: {
        autoplay: true,
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
        },
      },
      skuId: 0,
      albumPics: [],
      clicked: null,
      serviceIds: [],
      skuPrice:0,
      skuName:'',
      activeIndex: '1',
      reviews:[]
    };
  },
  components: {
    swiper,
    swiperSlide,
    ProductParam,
    ServiceBar,
    Comment,
  },
  mounted() {
    this.getProductInfo();
    this.getCommentInfo();
  },
  methods: {
    handleSelect(key, keyPath) {
      this.activeIndex=key;
    },
    async getProductInfo() {
      const {data} = await getDetail(this.id);
      this.product=data;
      this.total=this.product.sellingPrice*this.count;
    },
    async getCommentInfo(){
      const {data}=await getComment(this.id);
      this.reviews=data;
    },
    addToCart() {
      addCart({goodsId:this.product.goodsId,goodsCount:this.count}).then(() => {
          this.$message.success({ 
          dangerouslyUseHTMLString: true,
          message:"添加购物车成功，请点击购物车查看"});
        });
    },
    handleChange(value) {
      this.total=this.product.sellingPrice*value;
    },
    handlerSkuid(item, index) {
      if((item.stock-item.lockStock)<=0){
        this.$message.warning("库存不足，请选择其他规格");
        return;
      }
      // 重复点击则失效
      if (this.clicked == index) {
        this.clicked = null;
        this.skuId = 0; 
        this.albumPics = this.product.albumPics.split(",");
        this.albumPics.unshift(this.product.pic); 
        this.skuName='';
        this.skuPrice=0;
      } else {
        this.clicked = index;
        this.skuId = item.id;
        this.albumPics = [item.pic];
        this.skuName=this.product.name+ item.sp1+item.sp2+"<br/>库存："+(item.stock-item.lockStock)+this.product.unit+"--"+item.id;
        this.skuPrice=item.price;
      }
    },
    randomString(e) { 
    //形参e,需要产生随机字符串的长度   
    //如果没有传参，默认生成32位长度随机字符串
      e = e || 32;
      //模拟随机字符串库
      var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
      a = t.length,//字符串t的长度，随机数生成最大值
      n = "";
      for (var i = 0; i < e; i++) {
        //随机生成长度为e的随机字符串拼接
          n += t.charAt(Math.floor(Math.random() * a));
      }
      //返回随机组合字符串
      return n
      }

  },
};
</script>
<style lang="less" scoped>
.detail {
  .stock {
    margin-top: 15px;
    color: #1baeae;
  }
  .wrapper {
    .swiper {
      float: left;
      width: 520px;
      height: 520px;
      margin: 5px 20px 20px 0px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .content {
      float: right;
      width: 584px;
      height: auto;
      span {
        font-size: 18px;
      }
      .item-title {
        font-size: 27px;
        padding-top: 14px;
        padding-bottom: 14px;
        height: 100px;
      }
      .item-info {
        font-size: 18px;
        height: 30px;
        line-height: 30px;
      }
      .delivery {
        font-size: 18px;
        color: #1baeae;
        margin-top: 35px;
        margin-bottom: 14px;
        height: 20px;
      }
      .item-price {
        font-size: 20px;
        color: #1baeae;
        margin-top: 35px;
        height: 20px;
        .del {
          font-size: 16px;
          color: #999999;
          margin-left: 10px;
          text-decoration: line-through;
        }
      }
      .btn-group {
        width: 100%;
        height: 54px;
      }
      .after-sale-info {
        margin: 20px 0px;
        a {
          display: inline-block;
          margin-right: 55px;
          line-height: 30px;
          margin-bottom: 10px;
          cursor: default;
          white-space: nowrap;
          color: #b0b0b0;
        }
        i {
          font-size: 24px;
          display: inline-block;
          font-style: normal;
          vertical-align: middle;
        }
        em {
          font-style: normal;
          display: inline-block;
          margin-left: 2px;
          vertical-align: middle;
          font-size: 16px;
        }
      }
      .line {
        height: 0;
        margin-top: 25px;
        margin-bottom: 28px;
        border-top: 1px solid #e5e5e5;
      }
      .item-addr {
        height: 108px;
        background-color: #fafafa;
        border: 1px solid #e5e5e5;
        box-sizing: border-box;
        padding-top: 31px;
        padding-left: 64px;
        font-size: 14px;
        line-height: 14px;
        position: relative;
        .icon-loc {
          position: absolute;
          top: 27px;
          left: 34px;
          display:inline-block;
          width: 20px;
          height: 20px;
          background:url("/imgs/detail/icon-loc.png") no-repeat center;
          background-size: contain;
        }
        .addr {
          color: #666666;
        }
        .stock {
          margin-top: 15px;
          color: #1baeae;
        }
      }
      .item-version,
      .item-color {
        margin-top: 30px;
      }
      .item-version,
      .item-color {
        .phone {
          width: 50%;
          height: 50px;
          line-height: 50px;
          font-size: 14px;
          color: #333333;
          border: 1px solid #e5e5e5;
          box-sizing: border-box;
          text-align: center;
          cursor: pointer;
          span {
            color: #666666;
            margin-left: 15px;
          }
          .color {
            display: inline-block;
            width: 14px;
            height: 14px;
            background-color: #666666;
          }
          &.checked {
            border: 1px solid #1baeae;
            color: #1baeae;
          }
        }
        .disabled{
          color: #b0b0b0;
          cursor: auto;
        }
      }
      .item-total {
        height: 108px;
        background: #fafafa;
        padding: 24px 33px 29px 30px;
        font-size: 14px;
        margin-top: 50px;
        margin-bottom: 30px;
        box-sizing: border-box;
        .phone-total {
          font-size: 22px;
          color: #1baeae;
          margin-top: 18px;
        }
      }
    }
  }
  .price-info {
    text-align: center;
    background-color: #f3f3f3;
    height: auto;
    span {
      font-size: 16px;
      color: #333333;
      height: 50px;
      line-height: 50px;
    }
    .attributes-list {
      width: 749px;
      display: block;
      clear: both;
      margin: 20px auto;
      min-height: 50px;
      text-align: left;
      li {
        display: inline;
        float: left;
        width: 206px;
        height: 24px;
        margin-right: 20px;
        overflow: hidden;
        text-indent: 5px;
        line-height: 24px;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
}
</style>